package patrol.mobile.com.mobilepatrol.activity.culvert;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.HdjcxjcShqkb;
import patrol.mobile.com.mobilepatrol.bean.Hdjcxjcb;
import patrol.mobile.com.mobilepatrol.manager.HManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/10/5.
 */

public class CulvertOffenCheckBrokenDetailActivity extends BaseActivity{
    @BindView(R.id.item1)
    RelativeLayout rl1;

    @BindView(R.id.item2)
    RelativeLayout rl2;


    private EditText et1;
    private EditText et2;

    @BindView(R.id.bt)
    Button bt;
    //
    private String hdjcxjcid;
    //一个桥梁
    private String hdcode;
    private Hdjcxjcb hdjcxjcb;
    String id;
    private List<HdjcxjcShqkb> mList = new ArrayList<>();
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_offen_qs_hd_detail;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }

    @Override
    public void initView() {
        super.initView();


        TextView tv1 = rl1.findViewById(tv);
        tv1.setText("损坏、淤塞情况描述");
        et1 = rl1.findViewById(et);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("维修建议");
        et2 = rl2.findViewById(et);

    }

    @Override
    public void initData() {
        super.initData();
        hdjcxjcid = getIntent().getStringExtra("hdjcxjcid");
        hdcode = getIntent().getStringExtra("hdcode");
         id = getIntent().getStringExtra("id");
        hdjcxjcb = HManager.getCulvertJC(hdcode,hdjcxjcid);
        mList = hdjcxjcb.bjshpk;
        if (id!=null&&!id.equals("")){
            HdjcxjcShqkb bjqs = HManager.getCulvertJCbjqs(id);
            et1.setText(bjqs.shqkms);
            et2.setText(bjqs.wxjy);
        }

    }

    @Override
    public void setListener() {
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String shqkms = et1.getText().toString().trim();
                final String wxjy = et2.getText().toString().trim();
                if (shqkms.equals("")){
                    showToast("损坏、淤塞情况描述");
                    return;
                }
                if (wxjy.equals("")){
                    showToast("维修建议");
                    return;
                }
                if (id!=null&&!id.equals("")){

                    PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            RealmResults<HdjcxjcShqkb> br = realm.where(HdjcxjcShqkb.class).equalTo("id",id).findAll();
                            HdjcxjcShqkb HdjcxjcShqkb = br.get(0);
                            HdjcxjcShqkb.shqkms = shqkms;
                            HdjcxjcShqkb.wxjy = wxjy;
                        }
                    });
                }else{
                    PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {

                            HdjcxjcShqkb hdjcxjcShqkb = PatrolApp.realm.createObject(HdjcxjcShqkb.class);

                            hdjcxjcShqkb.id=CommonUtil.getUUID();
                            hdjcxjcShqkb.hdjcxjcid = hdjcxjcid;
                            hdjcxjcShqkb.shqkms = shqkms;
                            hdjcxjcShqkb.wxjy = wxjy;
                            hdjcxjcb.bjshpk.add(hdjcxjcShqkb);
                            mList = hdjcxjcb.bjshpk;
                        }
                    });
                }
                HManager.setHDJCUpState(hdjcxjcid,false);
                HManager.setHDUpState(hdcode,false);
                Intent mIntent = new Intent();
                // 设置结果，并进行传送
                setResult(200, mIntent);
                CommonUtil.exitActivityAndBackAnim(CulvertOffenCheckBrokenDetailActivity.this,true);
            }
        });
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CommonUtil.exitActivityAndBackAnim(CulvertOffenCheckBrokenDetailActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("涵洞经常检查缺损详情");
    }
}
